<?php
define('CHARSET', 'utf-8'); //网站编码
@header("Content-type: text/html; charset=" . CHARSET);

/*
 * 根据远程地址抓取页面 css && js 及养生图片信息
 * @Author zhouyang
 * @date 2012-09-21
 *
 * code 200 201 202 203 204 205
 */

Class takeHtmlCssJsImg
{
    public $http_url  = null;
    public $dir       = null;
    public $page_name = null;

    //定义一个构造方法
    public function __construct($http_url,$dir,$page_name)
    {
        $this->http_url  = $http_url;
        $this->dir       = $dir;
        $this->page_name = $page_name;
    }

    public function init(){
        $this->index();
        $this->getHtml();
        $this->getCss();
        $this->getImg();
        $this->getJs();
    }

    public function index(){

        $ret_data = array();

        if($this->http_url && $this->dir && $this->page_name){

            //加载解析html结构插件
            include "simple_html_dom.php";

            $html = file_get_html($this->http_url);

            // Find all script
            $script_arr = array();
            foreach ($html->find('script') as $element) {
                $script_arr[] = $element->src;
            }

            // Find all link
            $link_arr = array();
            foreach ($html->find('link') as $element) {
                $link_arr[] = $element->href;
            }

            $css_js_arr = array(
                'link'   => $link_arr,
                'script' => $script_arr
            );

            //下载使用
            $this->css_js_arr = $css_js_arr;

            $ret_data['code'] = 200;
            $ret_data['msg']  = $css_js_arr;

        }else{
            if(!$this->http_url && !$this->dir && !$this->page_name){
                $ret_data['code'] = 201;
                $ret_data['msg'] = 'Page or Dir or Page_name is null!';
            }else{
                if (!$this->http_url) {
                    $ret_data['code'] = 202;
                    $ret_data['msg']  = 'Page is null!';
                }
                if (!$this->dir) {
                    $ret_data['code'] = 203;
                    $ret_data['msg']  = 'Dir is null!';
                }
                if (!$this->page_name) {
                    $ret_data['code'] = 204;
                    $ret_data['msg']  = 'Page_name is null!';
                }
            }
        }

        return $ret_data;
    }

    public function getHtml(){
        echo '<br>************************************************************************<br><br>';
        $check_file = $this->checkFileExists($this->dir . '/' . $this->page_name);

        if ($check_file) {
            echo "<font class='blue_color'>The file $this->dir/$this->page_name exists</font><br>";
        } else {
            $ret = $this->createDir($this->dir);

            if ($ret) {
                echo "Create The file $this->dir success<br>";

                $copy_ret = copy($this->http_url, $this->dir . '/' . $this->page_name);

                if ($copy_ret) {
                    echo "Copy The file $this->http_url to $this->dir/$this->page_name success<br>";
                } else {
                    echo "<font class='red_color'>Copy The file $this->http_url faile</font><br>";
                }
            } else {
                echo "<font class='red_color'>Create The file $this->dir faile</font><br>";
            }
        }
    }

    public function getCss(){
        // Find all link
        echo '<br>************************************************************************<br><br>';
        $link_null_num = 0;
        foreach ($this->css_js_arr['link'] as $k => $v) {
            if ($v && $v != '/favicon.ico' && strpos($v,'.css')) {
                $ret = copy($v, $this->dir . '/css/' . $this->retrieve($v));

                if ($ret) {
                    echo "Copy The file $v success<br>";
                } else {
                    echo "<font class='red_color'>Copy The file $v faile.</font><br>";
                }
            } else {
                echo "<font class='green_color'>Do not Copy The file $v.</font><br>";
                $link_null_num++;
            }
        }
    }

    public function getJs(){
        // Find all script
        echo '<br>************************************************************************<br><br>';
        $script_null_num = 0;
        foreach ($this->css_js_arr['script'] as $k => $v) {
            if ($v) {
                $ret = copy($v, $this->dir . '/js/' . $this->retrieve($v));

                if ($ret) {
                    echo "Copy The file $v success<br>";
                } else {
                    echo "<font class='red_color'>Copy The file $v faile.</font><br>";
                }
            } else {
                echo "<font class='green_color'>Do not Copy The file script tabs.</font><br>";
                $script_null_num++;
            }
        }
    }

    public function getImg(){
        echo '<br>************************************************************************<br><br>';
        foreach ($this->css_js_arr['link'] as $k => $v) {
            if ($v && $v != '/favicon.ico' && strpos($v,'.css')) {
                $data = file_get_contents($v);

                $regex = '/url\(\'{0,1}\"{0,1}(.*?)\'{0,1}\"{0,1}\)/';
                preg_match_all($regex, $data, $result);

                $result = array_unique($result[1]);

                foreach($result as $__k=>$__v){
                    $result[$k] = str_replace('http://www.qiyipic.com','',$v);
                }

                $true_arr = array();
                $false_arr = array();

                foreach ($result as $val) {
                    if (preg_match('/^http.*/', $val)) {
                        $target = $val;
                    } else {
                        $target = str_replace('../', '', $val);
                    }

                    preg_match('/.*\/(.*\.\D+)$/', $val, $name);

                    $dir1 = '';
                    $dir2 = '';
                    $dir = '/';

                    if (@fopen($target, 'r')) {
                        $true_arr[] = $target;

                        echo $this->dir . '/' . dirname($dir1);
                        $this->createDir($this->dir . '/' . dirname($dir1));

                        if (!is_file($this->dir .'/'. $dir . $name[1])) {
                            $ret = copy($target, $this->dir . '/' . $dir . $name[1]);

                            if ($ret) {
                                echo "Copy The file $target success<br>";
                            } else {
                                echo "<font class='red_color'>Copy The file $target faile.</font><br>";
                            }
                        } else {
                            echo "<font class='red_color'>Copy The file $target name is error.</font><br>";
                        }
                    } else {
                        $false_arr[] = $target;
                        echo "<font class='red_color'>Open The file $target faile.</font><br>";
                    }
                }
                //echo '<br>成功下载图片：<br><br>';
                //print_r(array_unique($true_arr));

                //echo '<br>下载失败图片：<br><br>';
                //print_r(array_unique($false_arr));
            }
        }
    }

    /*
     * 检查文件是否已经抓取
     * @parame $file_path string 文件本地存储地址
     */
    public function checkFileExists($file_path)
    {
        if (file_exists($file_path)) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    /*
    * 创建文件夹
    */
    public function createDir($dir, $mode = 0777)
    {
        $ret = is_dir($dir) or ($this->createDir(dirname($dir)) and mkdir($dir, $mode));

        if ($ret) {
            return TRUE;
        }
        return FALSE;
    }

    /*
     * 获取http地址文件名
     */
    public function retrieve($url)
    {
        preg_match('/\/([^\/]+\.[a-z]+)[^\/]*$/', $url, $match);
        return $match[1];
    }
}


/*
 * 测试
 */
$http_url = isset($_REQUEST['http_url']) ? $_REQUEST['http_url'] : '';
$dir = isset($_REQUEST['dir']) ? $_REQUEST['dir'] : '';
$page_name = isset($_REQUEST['page_name']) ? $_REQUEST['page_name'] : '';

$down_arr = array(
    0=>array(
        'http_url'=>'http://m.iqiyi.com/index.html',
        'dir' => 'iqiyi',
        'page_name' => 'index.html'
    )
);

if($down_arr){

    foreach((array)$down_arr as $k=>$v){
        $takeHtmlCssJsImg = new takeHtmlCssJsImg($v['http_url'],$v['dir'],$v['page_name']);

        $takeHtmlCssJsImg->init();
    }
}


